package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ApartmentLabelService labelService;//公寓标签关联
    @Autowired
    private ApartmentFacilityService facilityService;//公寓配套关联
    @Autowired
    private ApartmentFeeValueService feeValueService;//公寓杂费关联
    @Autowired
    private GraphInfoService graphInfoService;//图片管理
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Override
    public boolean saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate=apartmentSubmitVo.getId()!=null&&apartmentSubmitVo.getId()!=0;
        String province = provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName();
        String city=cityInfoService.getById(apartmentSubmitVo.getCityId()).getName();
        String district=districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName();
        apartmentSubmitVo.setProvinceName(province);
        apartmentSubmitVo.setCityName(city);
        apartmentSubmitVo.setDistrictName(district);
        super.saveOrUpdate(apartmentSubmitVo);
        LambdaQueryWrapper<ApartmentLabel> labelWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        if (isUpdate){
            //公寓标签删除
            labelWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            labelService.remove(labelWrapper);
            //公寓配套删除
            facilityWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            facilityService.remove(facilityWrapper);
            //公寓杂费删除
            feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            feeValueService.remove(feeValueWrapper);
            //图片列表删除
            graphInfoWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoWrapper);
        }
        //添加公寓标签
        if (apartmentSubmitVo.getLabelIds()!=null&&!apartmentSubmitVo.getLabelIds().isEmpty()){
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            List<ApartmentLabel> labels=new ArrayList<>(labelIds.size());
            for (Long id : labelIds) {
                labels.add(ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(id).build());
            }
            labelService.saveBatch(labels);
        }
        //添加公寓配套
        if (apartmentSubmitVo.getFacilityInfoIds()!=null&&!apartmentSubmitVo.getFacilityInfoIds().isEmpty()){
            List<Long> infoIds = apartmentSubmitVo.getFacilityInfoIds();
            List<ApartmentFacility> facilities=new ArrayList<>(infoIds.size());
            for (Long id : infoIds) {
                facilities.add(ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(id).build());
            }
            facilityService.saveBatch(facilities);
        }
        //添加公寓杂费
        if (apartmentSubmitVo.getFeeValueIds()!=null&&!apartmentSubmitVo.getFeeValueIds().isEmpty()){
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            List<ApartmentFeeValue> feeValues=new ArrayList<>(feeValueIds.size());
            for (Long id : feeValueIds) {
                feeValues.add(ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(id).build());
            }
            feeValueService.saveBatch(feeValues);
        }
        //添加公寓图片
        if (apartmentSubmitVo.getGraphVoList()!=null&&!apartmentSubmitVo.getGraphVoList().isEmpty()){
            List<GraphVo> voList = apartmentSubmitVo.getGraphVoList();
            List<GraphInfo> graphInfoList=new ArrayList<>(voList.size());
            for (GraphVo vo : voList) {
                GraphInfo graphInfo=new GraphInfo();
                graphInfo.setName(vo.getName());
                graphInfo.setUrl(vo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        return true;
    }

    @Override
    public void listApartments(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        page=apartmentInfoMapper.listApartments(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartment(Long id) {
        List<GraphVo> graphVos=graphInfoMapper.queryVo(id,ItemType.APARTMENT);
        List<LabelInfo> labelInfos=labelInfoMapper.queryInfo(id,ItemType.APARTMENT);
        List<FacilityInfo> facilityInfos=facilityInfoMapper.queryInfo(id,ItemType.APARTMENT);
        List<FeeValueVo> feeValueVos=feeValueMapper.queryVo(id);
        ApartmentInfo apartmentInfo = super.getById(id);
        ApartmentDetailVo apartmentDetailVo=new ApartmentDetailVo();
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper=new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        LambdaQueryWrapper<ApartmentLabel> labelWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        //公寓标签删除
        labelWrapper.eq(ApartmentLabel::getApartmentId,id);
        labelService.remove(labelWrapper);
        //公寓配套删除
        facilityWrapper.eq(ApartmentFacility::getApartmentId,id);
        facilityService.remove(facilityWrapper);
        //公寓杂费删除
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        feeValueService.remove(feeValueWrapper);
        //图片列表删除
        graphInfoWrapper.eq(GraphInfo::getItemId,id);
        graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoWrapper);
        removeById(id);
    }
}




